import { JsonConfigHelper } from "./JsonConfigHelper";
import {BuildCfgs, LevelCollectionCfgs, CatteryFacilitiesCfgs, NoteCfgs, TaskCfgs, ConstantCfgs, PropCfgs, LevelRewardCfgs, CatShopCfgs,CatPretendCfgs } from "./ConfigStructures";
import { ConfigHelper } from "../app_base/module/config/ConfigHelper";



export enum ConfigPath {
    Bus = "ParkGame://Config/BusLevelCfgs",
    ChengYu = "ChengYuGame://Config/wordsCfgs",
    ChengYuLevel = "ChengYuGame://Config/wordsLevelCfgs",
    LvBead = "MainRes://Config/LevelBeadCfgs",
    LvBuild = "MainRes://Config/LevelBuildCfgs",
    LvBuildCollection = "MainRes://Config/LevelBuildCollectionCfgs",
    TurtleMain = "TurtleCollisionGame://Config/Turtle_main",
    TurtleRule = "TurtleCollisionGame://Config/Turtle_rule",
    Puzzle = "MainRes://Config/LevelPuzzleCfgs",
    ThrowItem_main = "ThrowItemGame://Config/main",
    ShootItem = "ShootItemGame://Config/main",

    CatteryFacilities = "ParkAndScrew://Config/CatteryFacilitiesCfgs",
    Shop = "ParkAndScrew://Config/CatShopCfgs",
    CatPretend = "ParkAndScrew://Config/CatPretendCfgs",
    Note = "ParkAndScrew://Config/NoteCfgs",
    Task = "ParkAndScrew://Config/TaskCfgs",
    Constant = "ParkAndScrew://Config/ConstantCfgs",
    Prop = "ParkAndScrew://Config/PropCfgs",
    LevelReward = "ParkAndScrew://Config/LevelRewardCfgs",
    ParkAScrew = "ParkAndScrew://Config/ParkAScrewLevelCfgs",
}
/**
* @desc 配置管理中心,支持两种配置初始化方式：硬编码（快捷访问方式）、动态添加（通过get函数动态创建）
* 
* @author timoo
* @date 2022/12/24
* @class ConfigManager
*/
class ConfigManager {
    private _helperMap: Map<ConfigPath, JsonConfigHelper<any>> = new Map();

    /**
    //  * [硬编码] 等级配置快捷访问方式
    //  */
    // get level(): JsonConfigHelper<Levels> {
    //     return this.get(ConfigPath.Level);
    // }

    // /**
    //  * [硬编码] 条件配置快捷访问方式
    //  */
    // get condition(): ConfigHelper<Condition> {
    //     return this.get(ConfigPath.Condition);
    // }

    /*
    * 停车和螺丝游戏配置
    */
    get parkAScrew(): JsonConfigHelper<BuildCfgs[]> {
        return this.get(ConfigPath.ParkAScrew);
    }

    /**
     * [硬编码] 建筑配置
     */
    get build(): JsonConfigHelper<BuildCfgs[]> {
        return this.get(ConfigPath.LvBuild);
    }

    /**
     * [硬编码] 建筑收集配置
     */
    get buildCollection(): JsonConfigHelper<LevelCollectionCfgs[]> {
        return this.get(ConfigPath.LvBuildCollection);
    }


    /**
     * [硬编码] 猫舍设施配置
     */
    get catteryFacilities(): JsonConfigHelper<CatteryFacilitiesCfgs[]> {
        return this.get(ConfigPath.CatteryFacilities)
    }

    /**
     * [硬编码] 商城配置
     */
    get shop(): JsonConfigHelper<CatShopCfgs[]> {
        return this.get(ConfigPath.Shop)
    }

    /**
     * [硬编码] 猫咪装扮配置
     */
    get catPretend(): JsonConfigHelper<CatPretendCfgs[]> {
        return this.get(ConfigPath.CatPretend)
    }

    /**
     * [硬编码] 游记配置
     */
    get note(): JsonConfigHelper<NoteCfgs[]> {
        return this.get(ConfigPath.Note)
    }

    /**
     *  [硬编码] 任务配置
     */
    get task(): JsonConfigHelper<TaskCfgs[]> {
        return this.get(ConfigPath.Task)
    }

    /**
     * [硬编码] 常数配置
     */
    get constant(): JsonConfigHelper<ConstantCfgs[]> {
        return this.get(ConfigPath.Constant)
    }

    /**
     * [硬编码] 道具配置
     */
    get prop(): JsonConfigHelper<PropCfgs[]> {
        return this.get(ConfigPath.Prop)
    }

    /**
     *[硬编码] 关卡奖励
     */
    get levelReward(): JsonConfigHelper<LevelRewardCfgs[]> {
        return this.get(ConfigPath.LevelReward)
    }


    /**
     * [硬编码] 颜色螺丝难度权重配置
     */
    get throwItem_main(): JsonConfigHelper<LevelRewardCfgs[]> {
        return this.get(ConfigPath.ThrowItem_main)
    }

    /**
     * [硬编码] 射击老鼠配置
     */
    get shootItem(): JsonConfigHelper<LevelRewardCfgs[]> {
        return this.get(ConfigPath.ShootItem)
    }

    public loadConfig(path) {
        return new Promise<JsonConfigHelper<any>>((resolve, reject) => {
            let helper: JsonConfigHelper<any> = this._helperMap.get(path);
            if (!helper) {
                helper = new JsonConfigHelper(path);
                this._helperMap.set(path, helper);
                helper.loadData(() => {
                    resolve(helper);
                }, this);
            } else {
                resolve(helper);
            }
        })
    }

    /**
     * 动态创建并获取配置
     * @param {ConfigPath} path  配置路径
     * @param {{new(path?:string):JsonConfigHelper}} [clazz] 配置类
     * @returns 
     */
    get<T extends JsonConfigHelper<any>>(path: ConfigPath, clazz?: { new(path?: string): JsonConfigHelper<any> }): T {
        let helper: JsonConfigHelper<any> = this._helperMap.get(path);
        if (!helper) {
            clazz = clazz || JsonConfigHelper;
            helper = new clazz(path);
            this._helperMap.set(path, helper);
        }
        return helper as T;
    }

    storgeMap = {}

    /**
     * 通过key获取配置
     * @param key 
     * @returns 
     */
    storgeConfig(bundleName, path) {
        let bundle = cc.assetManager.getBundle(bundleName)

        this.storgeMap[path] = bundle.get(path, cc.JsonAsset);
    }

    /**
     * 通过key获取配置
     * @param key 
     * @returns 
     */
    getStorgeConfig(key) {
        return this.storgeMap[key];
    }
}

/**
 * 配置管理中心,支持两种配置初始化方式：硬编码（快捷访问方式）、动态添加（通过get函数动态创建）
 */
export var G_Config = new ConfigManager();